package com.example.agendamesquita;

import java.util.Date;

import android.app.Activity;
import android.app.ActionBar;
import android.app.AlertDialog.Builder;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.os.Build;
import com.example.agendamesquita.*;

public class CadastroActivity extends Activity 
{
	static long dataMs;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cadastro);
		dataMs=getIntent().getExtras().getLong("data");
		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
		}
		
		/*String dataTeste=""+dataMs;
		TextView data = new TextView(this);
		data = (TextView)findViewById(R.id.dataTextView);
		System.out.println(data.toString());
		Date dataShow = new Date(dataMs);
		System.out.println(dataShow.toString());
		//Toast.makeText(getApplicationContext(), dataShow.toString(), Toast.LENGTH_SHORT).show();
		//data.setText("Data: "+ dataShow.getDate()+"/"+dataShow.getMonth()+"/"+dataShow.getYear());*/
		
		
		
		//Button addButton = (Button) findViewById(R.id.button1);
        //addButton.setOnClickListener(new OnClickListener() {
 			
 			//@Override
 			//public void onClick(View v) {

		
			//EditText eventoText = (EditText)findViewById(R.id.nomeEventoEditText);
			//String nomeEvento = eventoText.getText().toString();
		
			//TimePicker timePicker = (TimePicker) findViewById(R.id.horaTimePicker);
			//String horaEvento = ""+timePicker.getCurrentHour()+":"+timePicker.getCurrentMinute();
		
			//EditText localText = (EditText)findViewById(R.id.localEditText);
			//String localEvento = localText.getText().toString();
		
			//ExpandableListView alarmeList = (ExpandableListView)findViewById(R.id.tempoAlarmeExpandableList);
			//String tempoAlarme = alarmeList.toString();
		
			//EditText descricaoText = (EditText)findViewById(R.id.localEditText);
			//String descricaoEvento = descricaoText.getText().toString();
			
			//Date dataEvento = new Date(dataMs);
			//String dataHora =""+horaEvento+" | "+dataEvento.getDay()+"/"+dataEvento.getMonth()+"/"+dataEvento.getYear();
			
			//HelperDB conexaoDb = new HelperDB(getApplicationContext());
			
			//conexaoDb.InserirEvento(nomeEvento, dataHora, localEvento, descricaoEvento);
			
		
       //  }});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.cadastro, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
public static class PlaceholderFragment extends Fragment 
{
	EventoDataSource conexao;
	
	public PlaceholderFragment() {			
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_cadastro,
				container, false);
		
		Button addButton = (Button)rootView.findViewById(R.id.adicionarButton);
        addButton.setOnClickListener(new OnClickListener() {
 			
 			@Override
 			public void onClick(View v) {

		
			EditText eventoText = (EditText)v.getRootView().findViewById(R.id.nomeEventoEditText);
			String nomeEvento = eventoText.getText().toString();
		
			TimePicker timePicker = (TimePicker)v.getRootView().findViewById(R.id.horaTimePicker);
			String horaEvento = ""+timePicker.getCurrentHour()+":"+timePicker.getCurrentMinute();
		
			EditText localText = (EditText)v.getRootView().findViewById(R.id.localEditText);
			String localEvento = localText.getText().toString();
		
			//ExpandableListView alarmeList = (ExpandableListView)v.getRootView().findViewById(R.id.tempoAlarmeExpandableList);
			//String tempoAlarme = alarmeList.toString();
		
			EditText descricaoText = (EditText)v.getRootView().findViewById(R.id.localEditText);
			String descricaoEvento = descricaoText.getText().toString();
			
			Date dataEvento = new Date(dataMs);
			String dataHora =""+horaEvento+" | "+dataEvento.getDay()+"/"+dataEvento.getMonth()+"/"+dataEvento.getYear();
			Log.i("asd", nomeEvento+"/"+dataHora+"/"+localEvento+"/"+descricaoEvento);
			
			conexao = new EventoDataSource(v.getRootView().getContext().getApplicationContext());
			Evento evento = new Evento();
			
			evento.setNome(nomeEvento);
			evento.setDescricao(descricaoEvento);
			evento.setDiaHora(Utilidade.DateToString(dataEvento));
			evento.setLocal(localEvento);
			conexao.open();
			conexao.addEvento(evento);			
		
         }});
		return rootView;
	}
}
}
